package com.java.basis.collection;

import java.util.*;
import java.util.concurrent.CopyOnWriteArraySet;

/**
 * @author wangning
 * @date 2023/5/12 17:22
 * HashSet底层的数据结构就是HashMap
 * new HashSet().add("a");
 * public boolean add(E e) {
 *     return map.put(e, PRESENT)==null;
 * }
 * // Dummy value to associate with an Object in the backing Map
 * private static final Object PRESENT = new Object();
 */
public class SetNotSafeDemo {
    public static void main(String[] args) {
        new HashSet<>();
//        Set<String> set = new HashSet<>();
//        Set<String> set = Collections.synchronizedSet(new HashSet<>());
        Set<String> set = new CopyOnWriteArraySet<>();
        for (int i = 1; i <= 30; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 8));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
        //java.util.ConcurrentModificationException

    }
}
